<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="app_id" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入app_id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="app_key" prop="appKey">
        <el-input
          v-model="queryParams.appKey"
          placeholder="请输入app_key"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方厂商平台id（cfs_third_platform的platform_id）" prop="thirdId">
        <el-input
          v-model="queryParams.thirdId"
          placeholder="请输入第三方厂商平台id（cfs_third_platform的platform_id）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="对接区域编码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入对接区域编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="对接区域名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入对接区域名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="社会运营商id（cfs_society_operater的operater_id）" prop="operatorId">
        <el-input
          v-model="queryParams.operatorId"
          placeholder="请输入社会运营商id（cfs_society_operater的operater_id）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="社会运营中心名称" prop="thirdDataName">
        <el-input
          v-model="queryParams.thirdDataName"
          placeholder="请输入社会运营中心名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="社会运营中心电话 " prop="thirdDataTel">
        <el-input
          v-model="queryParams.thirdDataTel"
          placeholder="请输入社会运营中心电话 "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方编号前缀（设备等编号前缀）" prop="codePrefix">
        <el-input
          v-model="queryParams.codePrefix"
          placeholder="请输入第三方编号前缀（设备等编号前缀）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否在线（1在线，0离线）" prop="onLine">
        <el-input
          v-model="queryParams.onLine"
          placeholder="请输入是否在线（1在线，0离线）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="queryParams.expireTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="受限制的设备类型编码（逗号隔开）" prop="limitDeviceCode">
        <el-input
          v-model="queryParams.limitDeviceCode"
          placeholder="请输入受限制的设备类型编码（逗号隔开）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input
          v-model="queryParams.updateUser"
          placeholder="请输入修改人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="社会运营中心表id（cfs_third_control_center）" prop="thirdControlCenterId">
        <el-input
          v-model="queryParams.thirdControlCenterId"
          placeholder="请输入社会运营中心表id（cfs_third_control_center）"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cfs:user-app-config:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cfs:user-app-config:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="app_id" align="center" prop="appId" />
      <el-table-column label="app_key" align="center" prop="appKey" />
      <el-table-column label="第三方厂商平台id（cfs_third_platform的platform_id）" align="center" prop="thirdId" />
      <el-table-column label="对接区域编码" align="center" prop="areaCode" />
      <el-table-column label="对接区域名称" align="center" prop="areaName" />
      <el-table-column label="社会运营中心名称" align="center" prop="thirdDataName" />
      <el-table-column label="社会运营中心电话 " align="center" prop="thirdDataTel" />
      <el-table-column label="第三方编号前缀（设备等编号前缀）" align="center" prop="codePrefix" />
      <el-table-column label="是否在线（1在线，0离线）" align="center" prop="onLine" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cfs:user-app-config:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cfs:user-app-config:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserAppConfigForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserAppConfigApi, UserAppConfigVO } from '@/api/cfs/userappconfig'
import UserAppConfigForm from './UserAppConfigForm.vue'

/** 社会运营平台_app配置 列表 */
defineOptions({ name: 'UserAppConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserAppConfigVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  appId: undefined,
  appKey: undefined,
  thirdId: undefined,
  areaCode: undefined,
  areaName: undefined,
  operatorId: undefined,
  thirdDataName: undefined,
  thirdDataTel: undefined,
  codePrefix: undefined,
  onLine: undefined,
  expireTime: [],
  limitDeviceCode: undefined,
  status: undefined,
  createTime: [],
  createUser: undefined,
  updateUser: undefined,
  thirdControlCenterId: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserAppConfigApi.getUserAppConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserAppConfigApi.deleteUserAppConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserAppConfigApi.exportUserAppConfig(queryParams)
    download.excel(data, '社会运营平台_app配置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
